package com.wms.service.impl;

import com.wms.dto.LocationDTO;
import com.wms.dto.TaskDetailDTO;
import com.wms.dto.WarehouseDTO;
import com.wms.entity.StockTakingTaskDetailGroup;
import com.wms.mapper.ReviewAdjustmentsDetailGroupMapper;
import com.wms.mapper.ReviewAdjustmentsDetailListMapper;
import com.wms.mapper.StockTakingTaskDetailGroupMapper;
import com.wms.service.StockTakingTaskDetailGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StockTakingTaskDetailGroupServiceImpl implements StockTakingTaskDetailGroupService {

    @Autowired
    private StockTakingTaskDetailGroupMapper stockTakingTaskDetailGroupMapper;

    @Autowired
    ReviewAdjustmentsDetailGroupMapper reviewAdjustmentsDetailGroupMapper;

    @Override
    public StockTakingTaskDetailGroup queryDetailGroupByTaskCode(String taskCode) {
        // 1. 查询任务基本信息
        StockTakingTaskDetailGroup taskDetail = stockTakingTaskDetailGroupMapper.queryTaskDetailGroupByCode(taskCode);
        if (taskDetail == null) {
            return null;
        }


        // 2. 查询库区信息
        List<WarehouseDTO> warehouses = stockTakingTaskDetailGroupMapper.queryWarehouse(taskDetail.getTaskId());
        
        // 3. 为每个库区查询货架信息
        for (WarehouseDTO warehouse : warehouses) {
            List<LocationDTO> locations = stockTakingTaskDetailGroupMapper.queryLocation(taskDetail.getTaskId(), warehouse.getId());
            
            // 4. 为每个货架查询物品列表
            for (LocationDTO location : locations) {
                List<TaskDetailDTO> items = stockTakingTaskDetailGroupMapper.queryTaskDetail(taskDetail.getTaskId(), location.getId());
                location.setTaskDetailDTOList(items);
            }
            
            warehouse.setLocationDTOList(locations);
        }

        // 5. 设置库区列表到任务详情中
        taskDetail.setWarehouseList(warehouses);

        // 如果审核状态是3(已完成)或4(打回重盘)，则显示审核相关信息
        // 审核信息已经在主查询中获取，不需要额外查询
        // 当auditStatus为3或4时，auditUserName、auditDate、auditOpinion字段会自动显示
        return taskDetail;
    }

}
